import 'package:mvvm_provider/config/handle_state.dart';
import 'package:mvvm_provider/page/bookshelf/states/book_shelf_detail_state.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_model_entity.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_recommend_entity.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_series_entity.dart';
import 'package:mvvm_provider/page/home/data/repository/home_repository.dart';
import '../../../base/base_state.dart';
import '../../../base/base_view_model.dart';
import '../../../model/response_model.dart';

class BookShelfDetailViewModel extends BaseViewModel<BookShelfDetailState> {
  BookShelfDetailViewModel() : super(BookShelfDetailState());

  /// 请求全部数据
  Future<void> getData() async {
    /// 请求主数据
    ResponseModel responseMode1 = await HomeRepository.getMainData<CartoonModelData>();
    state.netState1 = HandleState.handle(responseMode1, successCode: 0);
    if (state.netState1 == NetState.dataSuccessState) {
      state.mainModel = responseMode1.data;
    }

    /// 请求系列数据
    ResponseModel responseModel2 = await HomeRepository.getSeriesData<CartoonSeriesData>();
    state.netState2 = HandleState.handle(responseModel2, successCode: 0);
    if (state.netState2 == NetState.dataSuccessState) {
      CartoonSeriesData? cartoonSeriesData = responseModel2.data;
      state.seriesList = cartoonSeriesData?.seriesComics ?? [];
    }

    /// 请求推荐数据
    ResponseModel responseModel3 = await HomeRepository.getRecommendData<CartoonRecommendData>();
    state.netState3 = HandleState.handle(responseModel3, successCode: 0);
    if (state.netState3 == NetState.dataSuccessState) {
      CartoonRecommendData? cartoonRecommendData = responseModel3.data;
      state.recommendList = cartoonRecommendData?.infos ?? [];
    }

    if (state.netState1 == NetState.dataSuccessState &&
        state.netState2 == NetState.dataSuccessState &&
        state.netState3 == NetState.dataSuccessState) {
      state.netState = NetState.dataSuccessState;
    } else {
      state.netState = NetState.errorShowRefresh;
    }

    notifyListeners();
  }
}
